package com.lunwen.view;

import com.lunwen.dao.UserDao;
import com.lunwen.utils.StringUtils;
import com.lunwen.view.component.Context;
import com.lunwen.view.component.MassageTip;
import com.lunwen.view.value.LoginValue;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ModifyPassFrame extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    private Context bod = new Context(400, 300);
    private JTextField oldPass = new JTextField();
    private JTextField newPass = new JTextField();
    private JButton ok = new JButton("修改密码");
    private Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    private UserDao dao = new UserDao();

    public ModifyPassFrame() {
        this.add(bod);
        this.setTitle("修改密码");
        oldPass.setBounds(40, 20, bod.getWidth()-80, 40);
        newPass.setBounds(40, 80, bod.getWidth()-80, 40);
        ok.setBounds(40, 140, bod.getWidth()-80, 40);
        ok.addActionListener(this);
        bod.add(oldPass, "旧密码", 120);
        bod.add(newPass, "新密码", 120);
        bod.add(ok, 20);
        this.setSize(bod.getWidth(), bod.getHeight());
        this.setResizable(false);
        this.setLocation((screen.width-this.getWidth())/2, (screen.height-this.getHeight())/2);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == ok) {
            if (!StringUtils.isEmpty(oldPass.getText()) && !StringUtils.isEmpty(newPass.getText())) {
                int result = dao.updatePassword(oldPass.getText(),newPass.getText());
                if (result > 0){
                    MassageTip.showMassage(null,"修改成功");
                }else {
                    MassageTip.showMassage(null,"修改失败");
                }
            }else {
                MassageTip.showMassage(null,"未知错误");
            }
        }

    }
}